//
//  JJotherViewController.m
//  JJHW_coreData
//
//  Created by lizhenqiang on 16/4/10.
//  Copyright © 2016年 HFKJ_LZQ. All rights reserved.
//

#import "JJotherViewController.h"
#import "JJotherBottomView.h"
#import "NewsFirstRecImageCell.h"
#import "NewsBigImageCell.h"
#import "NewsNoImageCell.h"
#import "NewsSmallImageCell.h"
#import "NewsRecoImageObj.h"
#import "NewsListObj.h"
#import "ZiXunDetailViewController.h"
#import "SearchViewController.h"
@interface JJotherViewController ()

@property(strong, nonatomic)NSMutableArray *bannarArray;
@property(strong, nonatomic)JJotherBottomView *mybottomView;
@end

@implementation JJotherViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self addBottom];
    NavBarItem *rightItem = [[NavBarItem alloc] init];
    rightItem.normalImage = [UIImage imageNamed:@"icon_search"];
    rightItem.selectImage = [UIImage imageNamed:@"icon_search"];
    rightItem.btnFrame = CGRectMake(0, 0, 19, 19);
    rightItem.type = NavBarItemTypeImage;
    [self setRightNavItemWith:rightItem];
    [self addLabelNoDataToView:self.tableView AndText:@"暂无数据" andFrame:kNoDataLabelFrame];
    _objArray = [NSMutableArray new];
    _splashChildObj = [[SplashChild alloc] init];
    _splashChildObj.pid = _splashObj.internalBaseClassIdentifier;
    _splashChildObj.level = 2;
    _splashChildObj.childIdentifier = @"";
    _splashChildObj.icon = @"";
    _splashChildObj.name = @"全部";
    _splashChildObj.nameWidth = 60;
    if (_splashObj.child.count>0) {
        self.tableView.frame = CGRectMake(0, 64, DeviceWidth, DeviceHeight-49-64);
        SplashChild *obj = [[SplashChild alloc] init];
        obj=_splashChildObj;
        [_objArray addObject:obj];
        for (SplashChild *obj in _splashObj.child) {
            [_objArray addObject:obj];
        }
        
        _mybottomView = [[JJotherBottomView alloc] initWithFrame:CGRectMake(0, DeviceHeight-49, DeviceWidth, 49)];
        __weak JJotherViewController *weakSelf = self;
        _mybottomView.block = ^(SplashChild *obj){
            //去做数据刷新
            _splashChildObj = obj;
            [weakSelf loadDataWithCurrentpage:0];
        };
        [_mybottomView setViewData:_objArray andTitle:self.titleForNav];
        [self.view addSubview:_mybottomView];
    }
    else
    {
        self.tableView.frame = CGRectMake(0, 64, DeviceWidth, DeviceHeight-64);
    }
    
    [self.tableView registerNib:[UINib nibWithNibName:@"NewsBigImageCell" bundle:nil]  forCellReuseIdentifier:@"NewsBigImageCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"NewsNoImageCell" bundle:nil]  forCellReuseIdentifier:@"NewsNoImageCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"NewsSmallImageCell" bundle:nil] forCellReuseIdentifier:@"NewsSmallImageCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"NewsFirstRecImageCell" bundle:nil] forCellReuseIdentifier:@"NewsFirstRecImageCell"];//
    _bannarArray = [NSMutableArray new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addRefreshHeaderAndFooterView:self.tableView];
    
}

-(void)addBottom
{
   
}
-(void)rightItemClick:(id)sender
{
    CDLog(@"进入搜索页面");
    
    SearchViewController *search = [[SearchViewController alloc] init];
    
    [self.navigationController pushViewController:search animated:YES];
}
-(void)loadDataWithCurrentpage:(NSInteger)currentpage
{
    self.currentPage = currentpage;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    if (currentpage==0) {
        [self getRecoImageReqeust];
    }
    [self getNewsListRequest];
    
}

-(void)getRecoImageReqeust
{//广告
    Person *_person = [Person sharePerson];
    NSString *cityId = [NSString isEmpty:_person.cityId]?@"":_person.cityId;
    NSString *childId = [NSString isEmpty:_splashChildObj.childIdentifier]?@"":_splashChildObj.childIdentifier;
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:_person.countryId,@"countryId",cityId,@"districtId",_splashChildObj.pid,@"pCategoryId",childId,@"categoryId", nil];
    //request_cityImage_WithParams
    [[CDAPIManager sharedManager] request_cityImage_WithParams:dic andIsSign:YES andBlock:^(id data,NSError *error,BOOL isSuccess){
        //根据他来看 是否显示 有推荐图的cell
        if (isSuccess) {
            NSArray *array = [data objectForKey:@"data"];
            [_bannarArray removeAllObjects];
            if (array.count>0) {
                
                for (NSDictionary *dic in array) {
                    NewsRecoImageObj *obj = [[NewsRecoImageObj alloc] initWithDictionary:dic];
                    [_bannarArray addObject:obj];
                }
            }
            self.labelNoData.hidden = (self.pageDataArr.count<1&&_bannarArray.count<1)?NO:YES;
            [self.tableView reloadData];
        }
        else
        {
            [_bannarArray removeAllObjects];
            [self.tableView reloadData];
        }
    }];
}
-(void)getNewsListRequest
{//资讯列表
    /*
     count:20,一次请求多少条记录
     offset:1,从第几条记录开始请求,偏移量
     pCategoryId:xxx,一级分类id
     categoryId:xxx二级分类id
     countryId:xxx国家id
     districtId:xxx城市id
     */
    Person *_person = [Person sharePerson];
    NSInteger offset = self.currentPage * kPageSize;
    NSInteger count = kPageSize;
    NSString *cityId = [NSString isEmpty:_person.cityId]?@"":_person.cityId;
    
    NSString *childId = [NSString isEmpty:_splashChildObj.childIdentifier]?@"":_splashChildObj.childIdentifier;
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:NSStringFromIntL(count),@"count",NSStringFromIntL(offset),@"offset",_splashChildObj.pid,@"pCategoryId",childId,@"categoryId",_person.countryId,@"countryId",cityId,@"districtId", nil];
    
    [[CDAPIManager sharedManager] request_newsList_WithParams:dic andIsSign:YES andBlock:^(id data,NSError *error,BOOL isSuccess){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        if (isSuccess) {
            
            NSDictionary *mydic = [data objectForKey:@"data"];
            NSArray *array = [mydic objectForKey:@"list"];
            if (self.currentPage == 0) {
                [self.pageDataArr removeAllObjects];
            }
            for (NSDictionary *dic in array) {
                NewsListObj *obj = [[NewsListObj alloc] initWithDictionary:dic];
                [self.pageDataArr addObject:obj];
                
            }
            
            NSInteger total = [[mydic objectForKey:@"total"] integerValue];
            NSInteger totalPage;
            if (total % kPageSize == 0) {
                totalPage = total/kPageSize;
            }
            else
            {
                totalPage = total/kPageSize + 1;
            }
            self.labelNoData.hidden = (self.pageDataArr.count<1&&_bannarArray.count<1)?NO:YES;
            [self setTotalPages:totalPage-1];
            [self.tableView reloadData];
        }
        else
        {
            self.labelNoData.hidden = (self.pageDataArr.count<1&&_bannarArray.count<1)?NO:YES;
        }
        [self endRefreshControlLoading];
    }];
}

//-(void)setNavTitleView
//{
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth-170, 30)];
//
//    self.navigationItem.titleView = view;
//}
-(void)setNavBar_rightItem
{
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    
}

#pragma mark - UITableView DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_bannarArray.count>0) {
        return 2;
    }
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
        {
            if (_bannarArray.count>0) {
                return 1;
            }
            else
            {
                return self.pageDataArr.count;
            }
        }
            break;
        case 1:
        {
            return self.pageDataArr.count;
        }
            break;
            
        default:
            break;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    switch (indexPath.section) {
        case 0:
        {
            if (_bannarArray.count>0) {
                
                NewsFirstRecImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsFirstRecImageCell"];
                cell.VC = self;
                [self configureCell:cell atIndexPath:indexPath andCellType:0];
                return cell;
            }
            else
            {
                NewsListObj *obj = self.pageDataArr[indexPath.row];
                switch (obj.cellType) {
                    case KcellTypeNoImage: {
                        {
                            NewsNoImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsNoImageCell"];
                            [self configureCell:cell atIndexPath:indexPath andCellType:KcellTypeNoImage];
                            return cell;
                        }
                        break;
                    }
                    case KcellTypeSmallImage: {
                        {
                            NewsSmallImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsSmallImageCell"];
                            [self configureCell:cell atIndexPath:indexPath andCellType:KcellTypeSmallImage];
                            return cell;
                        }
                        break;
                    }
                    case KcellTypeBigImage: {
                        {
                            NewsBigImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsBigImageCell"];
                            [self configureCell:cell atIndexPath:indexPath andCellType:KcellTypeBigImage];
                            return cell;
                        }
                        break;
                    }
                }
            }
            
        }
            break;
        case 1:
        {
            NewsListObj *obj = self.pageDataArr[indexPath.row];
            switch (obj.cellType) {
                case KcellTypeNoImage: {
                    {
                        NewsNoImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsNoImageCell"];
                        [self configureCell:cell atIndexPath:indexPath andCellType:KcellTypeNoImage];
                        return cell;
                    }
                    break;
                }
                case KcellTypeSmallImage: {
                    {
                        NewsSmallImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsSmallImageCell"];
                        [self configureCell:cell atIndexPath:indexPath andCellType:KcellTypeSmallImage];
                        return cell;
                    }
                    break;
                }
                case KcellTypeBigImage: {
                    {
                        NewsBigImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsBigImageCell"];
                        [self configureCell:cell atIndexPath:indexPath andCellType:KcellTypeBigImage];
                        return cell;
                    }
                    break;
                }
            }
        }
            break;
            
        default:
            break;
    }
    
    return nil;
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath andCellType:(KcellType)type
{
    switch (indexPath.section) {
        case 0:
        {
            if (_bannarArray.count>0) {
                
                cell.fd_enforceFrameLayout = YES ;
                NewsFirstRecImageCell *dataCell = (NewsFirstRecImageCell *)cell;
                [dataCell setCellData:_bannarArray];
            }
            else
            {
                
                switch (type) {
                    case KcellTypeNoImage: {
                        {
                            cell.fd_enforceFrameLayout = NO ; // Enable to use "-sizeThatFits:"
                            NewsNoImageCell *dataCell = (NewsNoImageCell *)cell;
                            NewsListObj *obj = self.pageDataArr[indexPath.row];
                            [dataCell setCellData:obj];
                            
                        }
                        break;
                    }
                    case KcellTypeSmallImage: {
                        {
                            cell.fd_enforceFrameLayout = YES ;
                            NewsSmallImageCell *dataCell = (NewsSmallImageCell *)cell;
                            NewsListObj *obj = self.pageDataArr[indexPath.row];
                            [dataCell setCellData:obj];
                        }
                        break;
                    }
                    case KcellTypeBigImage: {
                        {
                            cell.fd_enforceFrameLayout = YES ;
                            NewsBigImageCell *dataCell = (NewsBigImageCell *)cell;
                            NewsListObj *obj = self.pageDataArr[indexPath.row];
                            [dataCell setCellData:obj];
                        }
                        break;
                    }
                }
                
            }
            
        }
            break;
        case 1:
        {
            
            switch (type) {
                case KcellTypeNoImage: {
                    {
                        cell.fd_enforceFrameLayout = NO ; // Enable to use "-sizeThatFits:"
                        NewsNoImageCell *dataCell = (NewsNoImageCell *)cell;
                        NewsListObj *obj = self.pageDataArr[indexPath.row];
                        [dataCell setCellData:obj];
                        
                    }
                    break;
                }
                case KcellTypeSmallImage: {
                    {
                        cell.fd_enforceFrameLayout = YES ;
                        NewsSmallImageCell *dataCell = (NewsSmallImageCell *)cell;
                        NewsListObj *obj = self.pageDataArr[indexPath.row];
                        [dataCell setCellData:obj];
                    }
                    break;
                }
                case KcellTypeBigImage: {
                    {
                        cell.fd_enforceFrameLayout = YES ;
                        NewsBigImageCell *dataCell = (NewsBigImageCell *)cell;
                        NewsListObj *obj = self.pageDataArr[indexPath.row];
                        [dataCell setCellData:obj];
                    }
                    break;
                }
            }
            
        }
            break;
            
        default:
            break;
    }
    
}


#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            if (_bannarArray.count>0) {
                
                return [tableView fd_heightForCellWithIdentifier:@"NewsFirstRecImageCell" cacheByIndexPath:indexPath configuration:^(NewsFirstRecImageCell *cell) {
                    [self configureCell:cell atIndexPath:indexPath andCellType:0];
                }];
            }
            else
            {
                NewsListObj *obj = self.pageDataArr[indexPath.row];
                switch (obj.cellType) {
                    case KcellTypeNoImage: {
                        {
                            return [tableView fd_heightForCellWithIdentifier:@"NewsNoImageCell" cacheByIndexPath:indexPath configuration:^(NewsNoImageCell *cell) {
                                [self configureCell:cell atIndexPath:indexPath andCellType:obj.cellType];
                            }];
                        }
                        break;
                    }
                    case KcellTypeSmallImage: {
                        {
                            return [tableView fd_heightForCellWithIdentifier:@"NewsSmallImageCell" cacheByIndexPath:indexPath configuration:^(NewsSmallImageCell *cell) {
                                [self configureCell:cell atIndexPath:indexPath andCellType:obj.cellType];
                            }];
                        }
                        break;
                    }
                    case KcellTypeBigImage: {
                        {
                            return [tableView fd_heightForCellWithIdentifier:@"NewsBigImageCell" cacheByIndexPath:indexPath configuration:^(NewsBigImageCell *cell) {
                                [self configureCell:cell atIndexPath:indexPath andCellType:obj.cellType];
                            }];
                        }
                        break;
                    }
                }
            }
            
        }
            break;
        case 1:
        {
            NewsListObj *obj = self.pageDataArr[indexPath.row];
            switch (obj.cellType) {
                case KcellTypeNoImage: {
                    {
                        return [tableView fd_heightForCellWithIdentifier:@"NewsNoImageCell" cacheByIndexPath:indexPath configuration:^(NewsNoImageCell *cell) {
                            [self configureCell:cell atIndexPath:indexPath andCellType:obj.cellType];
                        }];
                    }
                    break;
                }
                case KcellTypeSmallImage: {
                    {
                        return [tableView fd_heightForCellWithIdentifier:@"NewsSmallImageCell" cacheByIndexPath:indexPath configuration:^(NewsSmallImageCell *cell) {
                            [self configureCell:cell atIndexPath:indexPath andCellType:obj.cellType];
                        }];
                    }
                    break;
                }
                case KcellTypeBigImage: {
                    {
                        return [tableView fd_heightForCellWithIdentifier:@"NewsBigImageCell" cacheByIndexPath:indexPath configuration:^(NewsBigImageCell *cell) {
                            [self configureCell:cell atIndexPath:indexPath andCellType:obj.cellType];
                        }];
                    }
                    break;
                }
            }
        }
            break;
            
        default:
            break;
    }
    
    return 0;
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_bannarArray.count>0) {
        
        switch (indexPath.section) {
            case 0:
            {
                
            }
                break;
            case 1:
            {
                NewsListObj *obj = self.pageDataArr[indexPath.row];
                ZiXunDetailViewController *detail = [[ZiXunDetailViewController alloc] init];
                detail.titleForNav = @"资讯详情";
                if (obj.cellType == KcellTypeBigImage) {
                    detail.shareImageStr = obj.recoImage;
                }
                else if (obj.cellType == KcellTypeSmallImage)
                {
                    detail.shareImageStr = obj.coverImage;
                }
                detail.newsId = obj.internalBaseClassIdentifier;
                [self.navigationController pushViewController:detail animated:YES];
            }
                break;
            default:
                break;
        }
    }
    else
    {
        NewsListObj *obj = self.pageDataArr[indexPath.row];
        ZiXunDetailViewController *detail = [[ZiXunDetailViewController alloc] init];
        detail.titleForNav = @"资讯详情";
        if (obj.cellType == KcellTypeBigImage) {
            detail.shareImageStr = obj.recoImage;
        }
        else if (obj.cellType == KcellTypeSmallImage)
        {
            detail.shareImageStr = obj.coverImage;
        }
        detail.newsId = obj.internalBaseClassIdentifier;
        [self.navigationController pushViewController:detail animated:YES];
    }
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
